﻿var treeobjlist = {
    init: function () {
        $('#tableid').treegrid({
            fit: true,
            nowrap: false,
            border: false,
            rownumbers: true,
            animate: true,
            toolbar: toolbar,
            idField: 'ObjectID',
            treeField: 'ObjectName',
            columns: [[
                    { title: 'ID', field: 'ObjectID', width: 80 },
					{ title: lang.name, field: 'ObjectName', width: 300 },
					//{ title: '父ID', field: 'PObjectID', width: 100, align: 'center' },
                    { title: lang.sequence, field: 'SN', width: 80, align: 'center' },
                    { title: lang.Remarks, field: 'ObjectTypeDesc', width: 350, align: 'center' }
					
                    //,
                    //{ title: '创建时间', field: 'CreateTime', width: 360 }
				]]
        });
        gettreelist();
    },
    search: function () {
        gettreelist();
    }
};
var toolbar = [{
    text: lang.add,
    iconCls: 'icon-add',
    handler: function () { openPage("treeobjedit", "typeid=" + $.getPara('typeid'), "", lang.add_node); }
}, '-', {
    text: lang.modify,
    iconCls: 'icon-edit',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            openPage("treeobjedit", "objid=" + obj.ObjectID + "&typeid=" + $.getPara('typeid'), "", lang.modify_node);
        }
        else {
            $.alert(lang.select_node);
        }
    }
}, '-', {
    text: lang.delete_,
    iconCls: 'icon-remove',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            var _objid = obj.ObjectID;
            var isParent = false;
            for (var i = 0; i < _menudata.length; i++) {
                if (_menudata[i].PObjectID == _objid) {
                    isParent = true;
                }
            }
            if (isParent) { $.alert(lang.The_current_node_has_child_nodes_please_delete_child_nodes); }
            else {
                $.extend($.messager.defaults, {
                    ok: lang.ok,
                    cancel: lang.cancel
                });
                $.confirm(lang.Are_you_sure_to_delete_the_current_node, lang.delete_, function (b) {
                    if (b) {
                        deletetree(_objid);
                    }
                });
            }
        }
        else {
            $.alert(lang.select_node);
        }
    }
}];

var _menudata = [];
//#region 加载菜单数据
function gettreelist() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryAllTree",
        model:function(){return  parseInt($.getPara('typeid'));},
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PObjectID == '0') {
                        var obj = {};
                        obj.ObjectID = data[i].ObjectID;
                        obj.ObjectName = data[i].ObjectName;
                        obj.PObjectID = data[i].PObjectID;
                        obj.ObjectTypeDesc = data[i].ObjectTypeDesc;
                        obj.SN = data[i].SN;
                        obj.SH_SQL = data[i].SH_SQL;
                        obj.CreateTime = data[i].CreateTime;
                        obj.IsValid = data[i].IsValid;
                        getsubtree(data, data[i].ObjectID, obj);
                        _data.push(obj);
                    }
                }
                _menudata = data;
                $('#tableid').treegrid('loadData', _data);
            }

        }
    });

    function getsubtree(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PObjectID == _pid) {
                    var obj = {};
                    obj.ObjectID = _d[i].ObjectID;
                    obj.ObjectName = _d[i].ObjectName;
                    obj.PObjectID = _d[i].PObjectID;
                    obj.ObjectTypeDesc = _d[i].ObjectTypeDesc;
                    obj.SN = _d[i].SN;
                    obj.SH_SQL = _d[i].SH_SQL;
                    obj.CreateTime = _d[i].CreateTime;
                    obj.IsValid = _d[i].IsValid;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubtree(_d, _sdata[j].ObjectID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

function deletetree(ObjectID) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "EditTreeOjbect",
        model: function () { return $.toJSON({ 'action': 0, 'ObjectID': ObjectID,TypeID: $.getPara('typeid') }); },
        success: function (data) {
            $.alert(lang.Successfully_deleted, null, function () { treeobjlist.search(); });
        }
    });
}